// pages/jigou/jigou.js

import {
  ajax,
  src,
  util
} from "../../utils/util.js";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 0 列表 1地图
    moshi:0,
    yijiindex:0,
    erjiindex: -1,
    jibieindex:'',
    paixuindex:1,
    showaddresstc:false,
    list:'11',
    longitude:0,
    latitude:0,

    currentpage: 1,
    nogd: false,
    canjiazai: true,
    detailsindex:'',

    jibielist: ['综合医院', '三甲', '二甲', '二乙', '未评级', '三级', '二级', '一级']
  },
  tokaiqi:function(){
    console.log("开启定位")
  },

  tosearchjigou:function(){
    wx.navigateTo({
      url: '/pages/searchjigou/searchjigou',
    })
  },

   tomap:function(){
    // console.log(this.data.mdetails.longitude)
    // wx.navigateTo({
    //   url: '/pages/map/map?longitude=' + this.data.mdetails.longitude + "&latitude=" + this.data.mdetails.latitude,
    // })
    var detailsindex = this.data.detailsindex
    var latitude = Number(this.data.list[detailsindex].latitude)
    var longitude = Number(this.data.list[detailsindex].longitude)
    var name = this.data.list[detailsindex].name
    var address = this.data.list[detailsindex].address
    wx.openLocation({
      latitude,
      longitude,
      scale: 18,
      name,
      address
    })
  },

  // 滚动加载触底触发
  lower: function () {
    console.log('jiazai')

    if (this.data.canjiazai) {
      this.setData({
        canjiazai: false
      })
      if (this.data.nogd == false) {
        var currentpage = this.data.currentpage
        console.log(currentpage)
       
        this.getlist(currentpage+1)

        this.setData({
          currentpage: this.data.currentpage + 1
        })

      } else {
        console.log("没有更多")
      }
    } else {
      console.log("不能加载")
    }
  },


  //康复机构详情
  mechClick: function (e) {
    var uqid=this.data.uqid
    if(uqid){
      var id = e.currentTarget.dataset.id;
      // wx.navigateTo({
      //   url: '/pages/mechanismdetails/mechanismdetails?id=' + id,
      // })

      wx.navigateTo({
        url: '../mechanismdetails/mechanismdetails?id=' + id,
      })
    }else{
      wx.showToast({
          title: '请先授权登录',
          icon: 'none',
          duration: 2000,
          success:this.tomine()
      })
    }
  },



  // sleep(delay) {
  //   var start = (new Date()).getTime();
  //   while ((new Date()).getTime() - start < delay) {
  //     continue;
  //   }
  // },

  tomine:function(){
    // this.sleep(2000)
    wx.switchTab({
      url:"/pages/mine/mine"
    })
  },

  // 选地区
  select:function(e){
    var index = e.currentTarget.dataset.index;
    this.setData({
      select: Number(index)
    })
    this.setData({
      showtc:true
    })
  },

  // 选择级别
  selectjb:function(e){
    var index = e.currentTarget.dataset.index;
    if(index==0){

    }
    this.setData({
      jibieindex: Number(index),
      detailsindex: '',
      currentpage: 1,
      nogd: false,
      canjiazai: true,

    })
    this.setData({
      showtc: false
    })
    this.totop()
    this.getlist(1)
  },

  totop: function (e) { // 一键回到顶部
    this.setData({
      topNum: this.data.topNum = 0
    });
  },

  // 选择排序
  selectpx:function(e){
    var index = e.currentTarget.dataset.index;
    this.setData({
      paixuindex: Number(index),
      currentpage: 1,
      nogd: false,
      canjiazai: true,
    })
    this.setData({
      showtc: false
    })

    this.totop()
    this.getlist(1)
  },


  getalldiqu:function(){
    ajax("/api/index/area_list", {
      data: {
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {
      
      var obj={
        name:'全部地区',
        province_id: "1",
        list:[
          {
            name:"全部地区",
            city_id: "1"
          }
        ]
      }
      res.data.unshift(obj)
  
      this.setData({
        alldiqu:res.data
      })
    })
  },

  // 获取全部列表
  getlist: function (page){
    var yijiindex=this.data.yijiindex
    var erjiindex = this.data.erjiindex
    var province_id
    var city

    if (yijiindex==0){
      province_id=''
      city = ''
    } else if (erjiindex == -1){
      province_id = this.data.alldiqu[yijiindex].province_id
      city = ''
    }
    else{

      province_id = this.data.alldiqu[yijiindex].province_id
      city = this.data.alldiqu[yijiindex].list[erjiindex].city_id
    }

    ajax("/api/index/mechanisms_list", {
      data: {
        lng: this.data.longitude,
       
        lat: this.data.latitude,
        province_id: province_id,
        city: city,
        level_name: this.data.jibieindex == 0 ? '' : this.data.jibieindex,
        sort: this.data.paixuindex,
        page:page,
        limit:20,
        is_map:this.data.moshi
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {
        // this.setData({
        //   list: res.data
        // })
      if (page == 1) {
        if (res.data.length > 0) {
          for(var x in res.data){
            var obj ={
              content: '',
              color: 'transparent',
              fontSize: 0,
              display: 'BYCLICK',
            }
            res.data[x].width ='33px'
            res.data[x].height = '45px'
            res.data[x].iconPath='../images/dituicon.png'
            res.data[x].callout=obj
          }

          this.setData({
           
            list: res.data
          })
        } else {
          // console.log(res.data.list.length)
          this.setData({
            list:'',
            nogd: true,
          })
        }
        if (res.data.length < 20 && res.data.length > 0) {
          this.setData({
            nogd: true,
          })
        }

      } else {
        var list = this.data.list

        if (res.data.length > 0) {
          for (var x in res.data){
            list.push(res.data[x])
          }
          this.setData({
            list: list,
            // currentpage: this.data.currentpage + 1,
            canjiazai: true,
          })
        } else {
          console.log(res.data.length)
          this.setData({
            nogd: true,
          })
        }
        if (res.data.length < 20 && res.data.length > 0) {
          this.setData({
            nogd: true,
          })
        }
      }
    })
  },

  //点击控件时
  bianhuaview:function(e){
    // console.log(e)
    this.setData({
      detailsindex:''
    })
  },

  //选择地区
  selectdiqu:function(e){
    var index = e.currentTarget.dataset.index;
    this.setData({
      yijiindex:index,
      erjiindex:-1,
     
    })

  },
  //选择具体地区
  selectjtdiqu:function(e){
    var index = e.currentTarget.dataset.index;
    this.setData({
      erjiindex: Number(index),
      detailsindex: '',
      currentpage: 1,
      nogd: false,
      canjiazai: true,
    })
    this.setData({
      showtc: false
    })

    this.totop()
    this.getlist(1)
  },

  // 切换模式
  switchmoshi:function(){

    if (this.data.moshi == 0){
      var that = this
      wx.getSetting({
        success(res) {
          console.log(res.authSetting['scope.userLocation'])
          if (res.authSetting['scope.userLocation']) {
            that.setData({
              showaddresstc: false
            })
            wx.getLocation({
              type: 'wgs84',
              success: (res) => {
                var latitude = res.latitude
                var longitude = res.longitude
                var speed = res.speed
                var accuracy = res.accuracy

                wx.setStorageSync("latitude", latitude)
                wx.setStorageSync("longitude", longitude)

                that.qiehuan()

              },
            })
          } else {
            that.setData({
              showaddresstc: true
            })
          }
        }
      })
    }else{
      this.qiehuan()
    }
  },

  qiehuan:function(){

    if (this.data.moshi == 0) {
      this.setData({
        latitude: wx.getStorageSync('latitude'),
        longitude: wx.getStorageSync('longitude')
      })
      this.setData({
        moshi: 1,
        showtc: false,
      })
      this.getlist(1)
    } else {
      this.setData({
        moshi: 0,
        showtc: false,
        currentpage: 1,
        nogd: false,
        canjiazai: true,
      })
      this.getlist(1)
    }
  },

  changeModalCancel:function(){
    this.setData({
      showaddresstc: false
    })
  },

  markertap: function(e) {
    console.log(e.markerId)
    var detailsindex
    for(var x in this.data.list){
      if (this.data.list[x].id == e.markerId){
        detailsindex=x
      }
    }

    this.setData({
      detailsindex: detailsindex
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getalldiqu()
    this.getlist(1)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var uqid = wx.getStorageSync('uqid')
    that.setData({
      uqid: uqid != '' || uqid != undefined?uqid:''
    })
  
    wx.getSetting({
      success(res) {
        console.log(res.authSetting['scope.userLocation'])
        if (res.authSetting['scope.userLocation']) {
          that.setjingwei()
        }else{
          that.setData({
            latitude: 0,
            longitude: 0
          })
        }
      },
    })

  },
  setjingwei:function(){
    var that=this
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        wx.setStorageSync("latitude", latitude)
        wx.setStorageSync("longitude", longitude)

        that.setData({
          latitude: wx.getStorageSync('latitude'),
          longitude: wx.getStorageSync('longitude')
        })
        that.getlist(1)
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})